package com.ocom.wxapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.ocom.common.entity.access.EntityMjAreaInfo;
import com.ocom.common.entity.human.EntityDeptInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.recode.AccessRecordVo;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.wxapi.client.AccessClientService;
import com.ocom.wxapi.client.HumanClientService;
import com.ocom.wxapi.client.MjRecodeClientService;
import com.ocom.wxapi.service.AccessRecordPageService;
import com.ocom.wxapi.vo.accessrecordpage.AreaListVo;
import com.ocom.wxapi.vo.accessrecordpage.PageFormatVo;
import com.ocom.wxapi.vo.accessrecordpage.QueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
public class AccessRecordPageServiceImpl implements AccessRecordPageService {

    @Autowired
    private MjRecodeClientService mjRecodeClientService;

    @Autowired
    private AccessClientService accessClientService;

    @Autowired
    HumanClientService humanClientService;
    //门禁记录（AccessRecordPage）
    //查询（AccessRecord/queryVo
    @Override
    public Result query(QueryVo queryVo) {
        AccessRecordVo onShowVo = new AccessRecordVo();
        onShowVo.setComId(Integer.valueOf(queryVo.getComId()));
        onShowVo.setPName(queryVo.getUserName());
        onShowVo.setBeginTime(DateUtil.getDateTime(DateUtil.addDays(new Date(),-7)));
        if(!CommonUtil.isNull(queryVo.getBeginTime())){
            onShowVo.setBeginTime(queryVo.getBeginTime());
        }
        onShowVo.setEndTime(queryVo.getEndTime());
        if(!onShowVo.getBeginTime().substring(0,7).equals(onShowVo.getEndTime().substring(0,7))){
            return ResultUtil.error(ResultEnum.Err_4142);
        }
        onShowVo.setAreaNo(queryVo.getAreaNo());
//        onShowVo.setDeptId(queryVo.getDeptId());
        onShowVo.setPage(queryVo.getPage());
        onShowVo.setSize(500);

        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();

        if(yoCiUser.getRole() == 23l ){
            Result<List<EntityDeptInfo>> listResult = humanClientService.getDeptList(yoCiUser.getComId(),Long.valueOf(yoCiUser.getDeptId()),0);
            if(listResult.getCode()==200){
                onShowVo.setDeptIds(listResult.getData().stream().map(EntityDeptInfo -> EntityDeptInfo.getDeptId()).collect(Collectors.toList()));
            }
            if(queryVo.getDeptId()!=null && onShowVo.getDeptIds().contains(queryVo.getDeptId())){
                List<Long> deptIds = new ArrayList<>();
                deptIds.add(queryVo.getDeptId());
                onShowVo.setDeptIds(deptIds);
            }
        }else{
            if(queryVo.getDeptId()!=null){
                onShowVo.setDeptId(queryVo.getDeptId().intValue());
            }
        }


        log.info("【GetAccessRecord】获取门禁记录 参数 【{}】",onShowVo);

        return  mjRecodeClientService.GetAccessRecord(onShowVo);
    }

    @Override
    public Result onShow(com.ocom.wxapi.vo.accessrecordpage.OnShowVo onShowVo) {
        Map<String, Object> map = new HashMap<>();
        PageFormatVo pageFormatVo = new PageFormatVo();
        pageFormatVo.setPageTitle("门禁记录");
        map.put("pageFormat",pageFormatVo);
        Result result = accessClientService.GetAreaInfo(onShowVo.getComId());
        List<AreaListVo> areaList = new ArrayList<>();
        if (result.getCode()==200){

            List<EntityMjAreaInfo> entityMjAreaInfos = JSON.parseArray(JSON.toJSONString(result.getData()), EntityMjAreaInfo.class);
            if (entityMjAreaInfos!=null &&  entityMjAreaInfos.size() > 0){
                for (EntityMjAreaInfo entityMjAreaInfo: entityMjAreaInfos) {
                    AreaListVo areaListVo = new AreaListVo();
                    areaListVo.setAreaNo(String.valueOf(entityMjAreaInfo.getAreaId()));
                    areaListVo.setAreaName(entityMjAreaInfo.getAreaName());
                    areaList.add(areaListVo);
                }
                map.put("areaList",areaList);
            }else {
                map.put("areaList","");
            }
        }

        map.put("nowTime", DateUtils.getTime());

        return ResultUtil.success(map);
    }


}
